/*
 * Project: greenline-rms-biz-dal
 * 
 * File Created at 2013幄17朄17旄17
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.pay.biz.dal.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

/**
 * @Type BaseDAO
 * @Desc DAO操作基础
 * @author Hero_Q
 * @date 2013
 * @Version V1.0
 */
public class BaseDAO extends SqlMapClientDaoSupport {

    /**
     * 分类查询（继承PageQuery，设置各自的查询条件，statementName_count是查询
     * 
     * @param statementName
     * @param query
     * @return
     */
    @SuppressWarnings("unchecked")
    protected <T> PageQuery<T> listByQuery(String statementName, PageQuery<T> query) {
        // 获取总记录数
        query.setTotalRecord(countByQuery(statementName + "_count", query));
        // 如果有记彄17
        if (query.getTotalRecord() > 0) {
            List<T> result = getSqlMapClientTemplate().queryForList(statementName, query);
            if (result == null) {
                result = new ArrayList<T>();
            }
            query.setDataList(result);
        }
        return query;
    };

    /**
     * 获取总数
     * 
     * @param statementName
     * @param query
     * @return
     */
    public <T> Integer countByQuery(String statementName, PageQuery<T> query) {
        return (Integer) getSqlMapClientTemplate().queryForObject(statementName, query);
    }

    /**
     * 获取总数
     * 
     * @param statementName
     * @param query
     * @return
     */
    public <T> Integer countByQuery(String statementName, Map<String, Integer> map) {
        return (Integer) getSqlMapClientTemplate().queryForObject(statementName, map);
    }

}
